<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';
	
	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}

	if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
	$id = intval($_REQUEST['ID']);
	$holidayName = $_REQUEST['HolidayName'];
	$date = date("Y-m-d", strtotime($_REQUEST['Date']));

	$sqlDBManager = new SqlDBManager();

	$sql = "SELECT * from Holiday where ID=?";
	$parameters = array($id);
	$res=$sqlDBManager->queryRow($sql,$parameters);

	if(!empty($res)){
		$idExisted=$res['ID'];
		$holidayNameExisted=$res['HolidayName'];
		$dateExisted=$res['Date'];

		//If no changes, return ture else execute update statement
		if($id==$idExisted&&$holidayName==$holidayNameExisted&&$date==$dateExisted){
			echo json_encode(array('success'=>true));	
		}else{
			$sql = "update holiday set HolidayName=?,Date=? where ID=?";
			$parameters = array($holidayName,$date,$id);
			$res=$sqlDBManager->execute_dml($sql,$parameters);

			//close connection
		  	$sqlDBManager->close_connect();

			if ($res>0){
				echo json_encode(array('success'=>true));
			} else {
				echo json_encode(array('msg'=>'Update holiday failed due to database error.'));
			}
		}
	}



?>